package com.yuanchu.store.controller;

import com.yuanchu.common.exception.BizCodeEnum;
import com.yuanchu.common.utils.PageResult;
import com.yuanchu.common.utils.R;
import com.yuanchu.common.validator.ValidationGroups;
import com.yuanchu.store.dto.*;
import com.yuanchu.store.entity.YoshopUserEntity;
import com.yuanchu.store.service.YoshopUserGradeService;
import com.yuanchu.store.service.YoshopUserService;
import com.yuanchu.store.utils.SecurityUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 用户记录表
 *
 * @author adan
 * @email 2028038953@gmail.com
 * @date 2023-12-19 20:57:21
 */
@RestController
@RequestMapping("shop/user")
public class ShopUserController {
    @Autowired
    private YoshopUserService userService;
    @Resource
    private YoshopUserGradeService userGradeService;
    /**
     * 列表
     */
    @GetMapping("/grade/all")
    public R userGrade(@RequestParam(required = false) Integer status){
        List<UserGradeDto> userGradeEntities = userGradeService.queryGradelist(status);
        Map<String, Object> map = new HashMap<>();
        map.put("list", userGradeEntities);
        return R.ok().put("data", map);
    }

    @ApiOperation("查询用户列表")
    @GetMapping("/list")
    public R queryUserList(QueryUserParamsDto param){
        PageResult<YoshopUserEntity> userList = userService.queryUserList(param);
        return R.ok().putPage("data", userList);
    }

    @ApiOperation("修改用户金额")
    @PostMapping("/recharge")
    public R editUserBalance(@RequestBody @Validated(ValidationGroups.Update.class) EditUserMoneyDto param){
       userService.editUserBalance(param);
        return R.ok("修改成功");
    }

    @ApiOperation("修改用户等级")
    @PostMapping("/grade")
    public R editUserGrade(@RequestBody @Validated(ValidationGroups.Update.class) EditUserParamsDto param){
        userService.editUserGrade(param);
        return R.ok("修改成功");
    }

    @ApiOperation("删除用户等级")
    @PostMapping("/delete")
    public R deleteUserGrade(@RequestBody @Validated(ValidationGroups.Delete.class) EditUserParamsDto param){
        userService.deleteUserGrade(param.getUserId());
        return R.ok("修改成功");
    }


    // @GetMapping("/count/day")
    // public Map<String, Object> queryNewUserByday(){
    //     return userService.queryNewUserByday();
    // }
    @GetMapping("/count/ids")
    public List<Integer> queryTotalUser(){
        return userService.queryUserIds();
    }

    @ApiOperation("查询用户信息")
    @GetMapping("/info")
    public R queryUserInfo() {
        Integer userId = SecurityUtil.getUserId();
        YoshopUserEntity userInfo = userService.getUserInfo(userId);
        Map<String, Object> map = new HashMap<>();
        map.put("userInfo", userInfo);
        // return R.ok().put("list", );
        return R.ok().put("data", map);
    }

    @ApiOperation("查询用户资产")
    @GetMapping("/assets")
    public R queryUserAsset() {
        Integer userId = SecurityUtil.getUserId();
        UserAssetDto assets = userService.getUserAsset(userId);
        Map<String, Object> map = new HashMap<>();
        map.put("assets", assets);
        // return R.ok().put("list", );
        return R.ok().put("data", map);
    }

    @ApiOperation("查询用户信息")
    @GetMapping("/{userId}")
    public YoshopUserEntity queryUserByUserId(@PathVariable Integer userId){
        YoshopUserEntity userEntity = userService.queryUserByUserId(userId);
        return userEntity;
    }

    @ApiOperation("更新用户余额")
    @PostMapping("/decr/balance")
    public R decrUserBalance(@RequestParam("userId") Integer userId, @RequestParam("payMoney") String payMoney,
                             @RequestParam("points") Integer points, @RequestParam("storeId") Integer storeId){
        boolean flag = userService.deductUserBalance(userId, payMoney, points, storeId);
        if (!flag) {
            return R.error(BizCodeEnum.DECR_USER_BALANCE_EXCEPTION.getCode(), BizCodeEnum.DECR_USER_BALANCE_EXCEPTION.getMsg());
        }
        return R.ok();
    }

    @ApiOperation("更新用户信息")
    @PostMapping("/personal")
    public R updateUserPersonal(@RequestBody EditUserParamsDto form){
        Integer userId = SecurityUtil.getUserId();
        EditUserParamsDto.UserParamsForm paramsForm = form.getForm();
        Integer avatarId = paramsForm.getAvatarId();
        String nickName = paramsForm.getNickName();
        boolean flag = userService.updateUserPersonal(userId, avatarId, nickName);
        if (!flag) {
            return R.error(BizCodeEnum.DECR_USER_BALANCE_EXCEPTION.getCode(), BizCodeEnum.DECR_USER_BALANCE_EXCEPTION.getMsg());
        }
        return R.ok("信息修改成功");
    }



}
